<style type="text/less" lang="less" scoped>
    .ff-matrix {
        min-height: 260px;
        max-width: 700px;
        padding: 20px;
        box-sizing: border-box;
        overflow: auto;
        &-title {
            font-size: 14px;
            color: #333;
            font-weight: 400;
        }
        &-tip {
            color: #FD6442;
            font-size: 12px;
            margin-left: 6px;
        }
        &-btn {
            text-align: center;
            margin-top: 17px;
        }
        &-table {
            margin-top: 20px;
            font-size: 14px;
            color: #333;
            font-weight: 400;
            &--bias {
                background: linear-gradient(19deg, transparent 49.5%, #DCDCDC 49.5%, #DCDCDC 50.5%, transparent 50.5%);
            }
            &--head {
                display: flex;
            }
            &-big-font{
                font-weight: bold;
            }
            &-color {
                width: 116px;
                height: 42px;
                border: 1px solid #DCDCDC;
                box-sizing: border-box;
                margin-top: -1px;
                display: flex;
                align-items: center;
                .ff-matrix-table-img {
                    position: relative;
                    display: inline-block;
                    width: 30px;
                    height: 30px;
                    margin: 0 10px 0 8px;
                    img {
                        height: 100%;
                    }
                    .ff-popover-big-img {
                        display: none;
                        position: absolute;
                        left: 40px;
                        top: -50%;
                    }
                    &:hover {
                        .ff-popover-big-img {
                            display: block;
                        }
                    }
                }
            }
            &-total {
                justify-content: center;
            }
            &-count {
                width: 69px;
                height: 42px;
                box-sizing: border-box;
                border: 1px solid #DCDCDC;
                margin-left: -1px;
                margin-top: -1px;
                display: flex;
                align-items: center;
                justify-content: center;
            }
            &-count-list {
                display: flex;
            }
            &-count-box {
                .ff-matrix-table-count-new {
                    flex: 1;
                    text-align: center;
                }
                .ff-matrix-table-count-change {
                    display: none;
                    word-wrap: break-word;
                    word-break: normal;
                    width: 28px;
                    height: 100%;
                    box-sizing: border-box;
                    overflow: hidden;
                    border-left: 1px solid #DCDCDC;
                    position: relative;
                    i {
                        font-size: 20px;
                        display: block;
                        text-align: center;
                        color: #ccc;
                        line-height: 1;
                    }
                    i:last-child {
                        width: 100%;
                        border-bottom: 1px solid #DCDCDC;
                        position: absolute;
                        transform: rotate(180deg);
                    }
                    .ff-matrix-table-count-active {
                        color: #666;
                    }
                }
                &:hover {
                    border-color: #378EEF;
                    position: relative;
                    z-index: 1;
                    .ff-matrix-table-count-change {
                        display: block;
                    }
                }
            }
        }
    }
</style>
<template>
    <div class="ff-matrix">
        <span class="ff-matrix-title">{{`${pro}${name}`}}<span class="ff-matrix-tip">(数量只能调低,不能调高)</span></span>
        <div class="ff-matrix-table">
            <ul class="ff-matrix-table--head">
                <li class="ff-matrix-table--bias ff-matrix-table-color"></li>
                <li v-for="size of sizes" :key="size.id" class="ff-matrix-table-count ff-matrix-table-big-font">{{size.label}}</li>
                <li class="ff-matrix-table-count ff-matrix-table-big-font">合计</li>
            </ul>
            <ul>
                <li v-for="(color, k) of items" :key="k" class="ff-matrix-table-count-list">
                    <div class="ff-matrix-table-color">
                        <el-popover
                            placement="right"
                            trigger="hover"
                            :popper-options="{ boundariesElement: 'body', gpuAcceleration: false }"
                        >
                            <div class="ff-popover-big-img">
                                <img :src="color.img.split('?')[0]" alt="" />
                            </div>
                            <span class="ff-matrix-table-img" slot="reference" @click="goCommodity(pro)">
                                <img :src="color.img" alt="" />
                            </span>
                        </el-popover>
                        <span class="ff-matrix-table-big-font">{{color.label}}</span>
                    </div>
                    <div
                        v-for="(count, j) of color.count"
                        :key="j"
                        class="ff-matrix-table-count ff-matrix-table-count-box"
                    >
                        <span class="ff-matrix-table-count-new">{{count.new}}</span>
                        <span class="ff-matrix-table-count-change">
                            <i
                                class="iconfont icon-shang"
                                :class="count.old > count.new? 'ff-matrix-table-count-active' : ''"
                                @click="countChange('add', k, j, count.old > count.new, count.new)"
                            ></i>
                            <i
                                class="iconfont icon-shang"
                                :class="count.new > 0? 'ff-matrix-table-count-active' : ''"
                                @click="countChange('subtract', k, j, count.new > 0, count.new)"
                            ></i>
                        </span>
                    </div>
                    <div class="ff-matrix-table-count">{{totalize(color.count, 1)}}</div>
                </li>
                <li class="ff-matrix-table-count-list">
                    <div class="ff-matrix-table-color ff-matrix-table-total ff-matrix-table-big-font">合计</div>
                    <div v-for="(size, k) of sizes" :key="size.id" class="ff-matrix-table-count">
                        {{totalize(items, 2, k)}}
                    </div>
                    <div class="ff-matrix-table-count">{{totalize(items, 3)}}</div>
                </li>
            </ul>
        </div>
        <div class="ff-matrix-btn">
            <button class="ff-common-btn" @click="changeCountCancel">取消</button>
            <button class="ff-common-btn ff-common-btn-confirm" @click="changeCountConfirm(totalize(items, 3))">确认</button>
        </div>
    </div>
</template>

<script>
    export default {
        props: {
            /* 商品编码 */
            pro: {
                type: String,
                default: ''
            },
            /* 商品名称 */
            name: {
                type: String,
                default: ''
            },
            /* 尺寸 */
            sizes: {
                type: Array,
                default: () => []
            },
            /* 尺寸和颜色对应数据 */
            items: {
                type: Array,
                default: () => []
            }
        },
        methods: {
            /* 数量加减 */
            countChange (style, k, j, status, count) {
                this.$emit('countChange', {
                    style,
                    k,
                    j,
                    status,
                    count
                })
            },
            /*计算sku数量变化时总数的变化 rank: 等级, index: 总计哪一列数据*/
            totalize (data, rank, index) {
                //单行
                if(rank === 1) {
                    return data.reduce((total, obj) => {
                        return total += obj.new;
                    }, 0)
                }
                //一列
                if(rank === 2) {
                    return data.reduce((total, obj) => {
                        return total += obj.count[index].new;
                    }, 0)
                }
                //总的
                return data.reduce((total, obj) => {
                    let totalize = obj.count.reduce((num, count) => {
                        return num += count.new;
                    }, 0)
                    return total += totalize;
                }, 0)
            },
            /*前往商品详情网站*/
            goCommodity (pro) {
                this.$router.push({
                    path: '/commodityDetails',
                    query: {
                        sku: pro
                    }
                })
            },
            /* 取消数量修改 */
            changeCountCancel () {
                this.$emit('changeCountCancel');
                this.$parent.showPopper = false;
            },
            /* 确认数量修改 */
            changeCountConfirm (total) {
                this.$emit('changeCountConfirm', total);
                this.$parent.showPopper = false;
            }
        }
    }
</script>
